from commands import add, admin
import commands

@admin
def clearowners(connection):
    connection.protocol.cleartier()
@admin
def refilltier(connection, value = None):
    if value == None:
        connection.send_chat('Usage: /refilltier #  (default is 2)')
        connection.send_chat('All tiers # and above get infinite blocks/ammo')
    else:
        connection.protocol.refill_tier = value
        connection.protocol.send_chat('All Tiers %s and greater now have infinite ammo')
@admin
def tier3(connection, player = None):
    if player == None:
        player = connection
    else:
        player = get_player(connection.protocol, player)
    player.buildtier=3
    connection.protocol.send_chat('%s has promoted %s to build tier %s' % (connection.name, player.name, player.buildtier))     
@admin
def promote(connection, player = None):
    if player == None:
        player = connection
    else:
        player = get_player(connection.protocol, player)
    if player.buildtier < 3:
        player.buildtier+=1
        connection.protocol.send_chat('%s has promoted %s to build tier %s' % (connection.name, player.name, player.buildtier))
    else:
        connection.send_chat('%s is already Tier 3!' % player.name)
@admin
def demote(connection, player = None):
    if player == None:
        player = connection
    else:
        player = get_player(connection.protocol, player)
    if player.buildtier > 0:
        player.buildtier-=1
        connection.protocol.send_chat('%s has demoted %s to build tier %s' % (connection.name, player.name, player.buildtier))
    else:
        connection.send_chat('%s is already Tier 0!' % player.name)
def tier(connection, player = None):
    if player == None:
        player = connection
    else:
        player = get_player(connection.protocol, player)
    connection.send_chat('%s is in build tier %s.' % (player.name, player.buildtier) )
    connection.send_chat('%s can %s' % (player.name,connection.protocol.tier_details[connection.buildtier]) )
add(promote)
add(demote)
add(tier)
add(tier3)
add(refilltier)
add(clearowners)

def apply_script(protocol, connection, config):
    class BuildtierConnection(connection):
        buildtier = 0
        def on_block_build_attempt(self, x, y, z):
            if self.buildtier >= self.protocol.refill_tier:
                self.refill()
            if self.buildtier == 0:
                self.send_chat("You cannot build!")
                return False
            if self.buildtier == 1 or self.buildtier == 2:
                self.protocol.tier_blocks[(x,y,z)] = self.name
            if self.buildtier > 2:
                self.protocol.tier_upperblocks.add((x,y,z))
            return connection.on_block_build_attempt(self, x, y, z)
        def on_block_destroy(self, x, y, z, mode):
            if self.buildtier == 0:
                self.send_chat("You cannot destroy blocks!")
                return False
            if self.buildtier == 1:
                if self.name == self.protocol.tier_blocks[(x,y,z)]:
                    del self.protocol.tier_blocks[(x,y,z)]
                    return connection.on_block_destroy(self, x, y, z, mode)
                else:
                    self.send_chat("You cannot destroy other people's or map blocks!")
                    return False
            if self.buildtier == 2:
                if self.name == self.protocol.tier_blocks[(x,y,z)]:
                    del self.protocol.tier_blocks[(x,y,z)]
                    return connection.on_block_destroy(self, x, y, z, mode)
                elif (x,y,z) in self.protocol.tier_upperblocks:
                    self.send_chat("You cannot destroy upper tier player's blocks!")
                    return False
                else: 
                    del self.protocol.tier_blocks[(x,y,z)]
                    return connection.on_block_destroy(self, x, y, z, mode)
            if self.buildtier == 3:
                if (x,y,z) in self.protocol.tier_upperblocks:
                    self.protocol.tier_upperblocks.discard((x,y,z))
                return connection.on_block_destroy(self, x, y, z, mode)

class BuildtierProtocol(protocol):
        refill_tier = 2
        tier_blocks = dict()
        tier_upperblocks = set()
        tier_details = []
        tier_details.append("look around but not place or destroy blocks.")
        tier_details.append("place and destroy their own blocks.")
        tier_details.append("place and destroy tier 2 and below blocks, as well as map blocks.")
        tier_details.append("place and destroy any blocks.")
        def cleartier(self):
            self.tier_blocks.clear()
            self.tier_upperblocks.clear()
            self.send_chat('Block ownership cleared.')
    return BuildtierProtocol, BuildtierConnection